package com.lxu.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.w3c.dom.Node;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 一次性扩展多行  解决样式丢失问题
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class WordComplex6 {

    @Test
    public void test() throws Exception {
        String path = "D:\\ComplexWord.docx";
        FileInputStream in = new FileInputStream(path);
        XWPFDocument hwpf = new XWPFDocument(in);
        List<XWPFTable> tables = hwpf.getTables();//获取word中所有的表格
        FileOutputStream out = new FileOutputStream("D:\\ComplexWord2.docx");
        XWPFTable table = tables.get(0);//获取第一个表格
        /*for (int i = 1; i <= 3; i++) {
            copy(table, i, i + 3, 1);
        }*/
        int extendRows=4;
        int startRow =1;
        int copyRows=3;
        for (int i = 1; i < extendRows; i++) {
            for (int j = startRow; j < startRow + copyRows; j++) {
                copy(table, j, j + copyRows, i);
            }
        }
        hwpf.write(out);
        out.flush();
        out.close();
    }

    public void copy(XWPFTable table, int sourceIndex, int rowIndex, int index) {
        XWPFTableRow sourceRow = table.getRow(sourceIndex);
        //在表格指定位置新增一行
        table.addRow(sourceRow,rowIndex);
        List<XWPFTableCell> cellList = table.getRow(rowIndex).getTableCells();
        if (null == cellList) {
            return;
        }
        //复制列及其属性和内容
        for (int i=0;i<cellList.size();i++) {
            setCellText(cellList.get(i), index);
        }
    }

    private void setCellText(XWPFTableCell targetCell, int index) {
        String text = targetCell.getText();
        if (StringUtils.isNotBlank(text)) {
            int begin = text.trim().indexOf("${") + 2;
            int end = text.trim().indexOf("}");
            if (begin >= 0 && end >= 0) {
                StringBuffer sb = new StringBuffer().append("${").append(assemblePlaceHolder(text.trim().substring(begin,end))).append(index).append("}");
                targetCell.getParagraphs().get(0).getRuns().get(0).setText(sb.toString(),0);
            }
        }
    }

    private String assemblePlaceHolder(String placeHolder) {
        if(StringUtils.isBlank(placeHolder)){
            return "";
        }
        if(isInteger(placeHolder.substring(placeHolder.length()-3))){
            return placeHolder.substring(0,placeHolder.length()-3);
        }
        if(isInteger(placeHolder.substring(placeHolder.length()-2))){
            return placeHolder.substring(0,placeHolder.length()-2);
        }
        if(isInteger(placeHolder.substring(placeHolder.length()-1))){
            return placeHolder.substring(0,placeHolder.length()-1);
        }
        return placeHolder;
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }


}
